// components/supplierSelection/index.js
const common = require('../../utils/common.js');
Component({
  options: {
    styleIsolation: 'shared',
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // 显示-隐藏
    show: {
      type: Boolean,
      value: false,
    },
    multiple: { // 是否多选
      type: Boolean,
      value: false,
    },
    options: {
      type: Array,
      value: [{
        label:'123',
        value:'123'
      }]
    },
    map: {
      type: Object,
      value: {
        value: 'value',
        label: 'label'
      }
    },
    value: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selectList: [], // 选中的数据
    supplyList: [], // 供应商列表
  },
  ready() {
    // 组件布局完成后执行
    this.getSupplyList();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //获取供应商列表
    getSupplyList() {
      this.setData({
        supplyList: this.data.options || [],
        selectList: this.data.value || []
      })
      this.resetSupplyList()
      console.log(this.data.options);
    },
    supplyCancelSelEvent(e) {
      let {
        selectedIndex
      } = e.detail;
      let list = this.data.selectList;
      selectedIndex.forEach((index, k) => {
        let removeElement = list.splice(index, 1);
        this.data.supplyList.unshift({
          ...removeElement[0]
        });
      });
      this.setData({
        ["selectList"]: list,
        supplyList: this.data.supplyList,
      });
      this.resetSupplyList();
      this.handleSelect()
    },
    //重置供应商对应数据
    resetSupplyList() {
      const value = this.data.map.value
      let supplyList = this.data.supplyList
      const selectList = this.data.selectList
      supplyList = supplyList.filter(i => {
        return !selectList.some(p => p[value] === i[value])
      })
      this.setData({
        supplyList: supplyList
      });
    },
  
    handelSearchConfirm() {
      this.setData({
        supplyList: []
      });
      this.getSupplyList();
    },
    
    //点击选择供应商
    supplySelEvent(e) {
      const value = this.data.map.value
      let sel_ids = e.detail.value;
      let selectList = this.data.selectList
      let supplyList = this.data.supplyList
      if (this.data.multiple) {
        for (let index = 0; index < sel_ids.length; index++) {
          let findindex = supplyList.findIndex(
            (v) => v[value] == sel_ids[index]
          );
          if (findindex != -1) {
            let findindex2 = selectList.findIndex(
              (v) => v[value] == sel_ids[index]
            );
            if (findindex2 == -1) {
              let is_sx = selectList.findIndex(
                (v) => v.firstChooseIndex == 1
              );
              selectList.push({
               ...this.data.supplyList[findindex]
              });
            }
          }
        }
      } else {
        if (selectList && selectList.length > 0) {
          supplyList.unshift({
            ...selectList[0]
          });
        }
        for (let index = 0; index < sel_ids.length; index++) {
          let findindex = supplyList.findIndex(
            (v) => v[value] == sel_ids[index]
          );
          if (findindex != -1) {
            let findindex2 = selectList.findIndex(
              (v) => v[value] == sel_ids[index]
            );
            if (findindex2 == -1) {
              let is_sx = selectList.findIndex(
                (v) => v.firstChooseIndex == 1
              );
              selectList = [{
                ...this.data.supplyList[findindex]
              }]
            }
          }
        }
      }
      this.setData({
        selectList: selectList,
        supplyList: supplyList
      });
      this.resetSupplyList();
      this.handleSelect()
    },
    //关闭选择供应商窗口
    closeSupplyEvent() {
      this.setData({
        show: false,
      });
      this.triggerEvent('close');
    },
    handleSelect() {
      const selectList = this.data.selectList
      if (this.data.multiple) {
        this.triggerEvent('handleSelect', selectList);
      } else {
        this.triggerEvent('handleSelect', selectList && selectList.length > 0 ? selectList[0] : {});
      }
    }
  }
})